package general.datastructures;

public class Rectangle {

	/**
	 * Left boundary of the Rectangle
	 */
	private float left;

	/**
	 * Right boundary of the Rectangle
	 */
	private float right;

	/**
	 * Top boundary of the Rectangle
	 */
	private float top;
	
	/**
	 * Bottom boundary of the Rectangle
	 */
	private float bottom;
	
	/**
	 * Simple Rectangle Class, which simply stores the Boundarys of the Rectangle, without errorchecks
	 * 
	 * @param left Left boundary of the Rectangle
	 * @param right Right boundary of the Rectangle
	 * @param top Top boundary of the Rectangle
	 * @param bottom Bottom boundary of the Rectangle
	 */
	public Rectangle(float left, float right, float top, float bottom) {
		this.left = left;
		this.right = right;
		this.top = top;
		this.bottom = bottom;
	}
	
	public Rectangle(Vector2f topLeft, float width, float height)
	{
		this.left = topLeft.x();
		this.right = this.left+width;
		this.top = topLeft.y();
		this.bottom = this.top+height;
	}
	
	public float getLeft()
	{
		return left;
	}
	
	public float getRight()
	{
		return right;
	}

	public float getTop()
	{
		return top;
	}
	
	public float getBottom()
	{
		return bottom;
	}
	
	public float getHeight()
	{
		return bottom-top;
	}
	
	public float getWidth()
	{
		return right-left;
	}
	
	/**
	 * Moves the Rectangle 
	 * @param x
	 * @param y
	 */
	public void move(float x, float y)
	{
		this.left += x;
		this.right += x;
		this.top += y;
		this.bottom += y;
	}
	
	/**
	 * Moves the Rectangle 
	 * @param x
	 * @param y
	 */
	public Rectangle move_cpy(float x, float y)
	{
		return new Rectangle(this.left + x, this.right + x, this.top + y, this.bottom += y);
	}
	
	public void move(Vector2f v)
	{
		move(v.x(), v.y());
	}
	
	public Rectangle move_cpy(Vector2f v)
	{
		return move_cpy(v.x(),v.y());
	}
	
	public void scale(float s)
	{
		float w = (getWidth()*s)/2;
		float h = (getHeight()*s)/2;
		
		left -= w;
		right += w;
		top -= h;
		bottom += h;
	}

	public Rectangle scale_cpy(float s)
	{
		float w = (getWidth()*s)/2;
		float h = (getHeight()*s)/2;
		
		return new Rectangle(left - w, right + w, top - h, bottom + h) ;
	}
	
	
	public Vector2f getOrigin()
	{
		return new Vector2f(left, top);
	}
	
	public boolean isInside(Vector2f v)
	{
		return (v.x()>=left && v.x()<=right && v.y()>=top && v.y()<=bottom); 
	}
	
	public boolean intersects(Rectangle rec)
	{
		if((this.left > rec.right) || (this.top > rec.bottom) || (this.right<rec.left) || (this.bottom  < rec.top))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
